package com.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.constant.MessageConstant;
import com.exception.CustomException;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.mapper.CheckItemMapper;
import com.pageResult.PageResult;
import com.pojo.CheckItem;
import com.query.QueryPageBean;
import com.result.Result;
import com.service.CheckItemService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Service(interfaceClass = CheckItemService.class)
public class CheckItemServiceImpl implements CheckItemService {

    @Autowired
    private CheckItemMapper checkItemMapper;
    //检查项列表(支持分页+条件)
    @Override
    public PageResult selectByQueryString(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize(),true);
        Page<CheckItem> page = checkItemMapper.selectByQueryString(queryPageBean.getQueryString());
        return new PageResult(page.getTotal(),page.getResult());
    }

    //检查项列表(暂时不支持分页)
    @Override
    public List<CheckItem> findAll() {
        return checkItemMapper.selectByExample(null);
    }

    //删除
    @Override
    public void delete(Integer id) throws CustomException {
        Integer count = checkItemMapper.isUseInCheckGroup(id);
        if(count>0){
            //有引用不能删除 抛出异常
            throw new CustomException(MessageConstant.CHECKITEM_IN_USE);
        }
        //删除
        checkItemMapper.deleteByPrimaryKey(id);
    }

    //添加
    @Override
    public void add(CheckItem checkItem) {
       checkItemMapper.insertSelective(checkItem);
    }
    //根据id查询
    @Override
    public CheckItem queryById(Integer id) {
        return checkItemMapper.selectByPrimaryKey(id);
    }
    //修改
    @Override
    public void update(CheckItem checkItem) {
        checkItemMapper.updateByPrimaryKeySelective(checkItem);
    }
}

